package cn.com.homeyoda.aimate.aihealth.views;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.bean.FamilyMembersBean;
import cn.com.homeyoda.aimate.aihealth.ui.health.adapter.MemberSelectAdapter;

public class MemberChangeDialog extends Dialog {

    RecyclerView rvMember;
    MemberSelectAdapter memberSelectAdapter;
    ArrayList<FamilyMembersBean.DataBean.FamilyMembers> familyMembers;
    private long mSelectUserId;
    SelectClickListener sureClickListener;
    private FamilyMembersBean.DataBean.FamilyMembers mSelectMember;

    public void setDatas(ArrayList<FamilyMembersBean.DataBean.FamilyMembers> familyMembersArrayList, long selectUserId) {
        familyMembers = familyMembersArrayList;
        mSelectUserId = selectUserId;
        if (memberSelectAdapter != null) {
            memberSelectAdapter.setSelectUserId(mSelectUserId);
            memberSelectAdapter.setNewData(familyMembers);
        }
    }

    public MemberChangeDialog(Context context,
                              ArrayList<FamilyMembersBean.DataBean.FamilyMembers> familyMembersArrayList,
                              long selectUserId,
                              SelectClickListener sureClickListener) {
        super(context, R.style.BlackDialog);
        familyMembers = familyMembersArrayList;
        mSelectUserId = selectUserId;
        this.sureClickListener = sureClickListener;
    }

    @Override
    public void setContentView(@NonNull View view) {
        //全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_member_select);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); // 设置全屏

        rvMember = findViewById(R.id.rv_member);

        rvMember.setLayoutManager(new GridLayoutManager(getContext(), 2));
        rvMember.addItemDecoration(new SpaceItemDecoration(0, 0, 20, 20));
        if (memberSelectAdapter == null) {
            memberSelectAdapter = new MemberSelectAdapter(R.layout.item_member_select, familyMembers, getContext());
            rvMember.setAdapter(memberSelectAdapter);
            memberSelectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    mSelectUserId = familyMembers.get(position).getId();
                    mSelectMember = familyMembers.get(position);
                    memberSelectAdapter.setSelectUserId(mSelectUserId);
                    memberSelectAdapter.notifyDataSetChanged();
                }
            });
        } else {
            memberSelectAdapter.setNewData(familyMembers);
        }

        findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sureClickListener.onSelectMember(mSelectMember);
                dismiss();
            }
        });
    }

    public static interface SelectClickListener {
        void onSelectMember(FamilyMembersBean.DataBean.FamilyMembers member);
    }

}
